package zabbix

import (
	"encoding/json"
	"errors"
	"fmt"
)

func HostCreateData() (string,map[string]interface{}) {
	method := "host.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "host": "Linux server4",
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "192.168.3.4",
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "2"
            }
        ],
        "templates": [
            {
                "templateid": "10001"
            }
        ],
        "inventory_mode": 0,
        "inventory": {
            "macaddress_a": "01234",
            "macaddress_b": "56768"
        }
	}`)
	json.Unmarshal((jsonstr),&data)
	return method,data
}

func HostCreateByoptionData(hostname string,hostip string,groupid string,templateid string) (string,map[string]interface{}) {
	method := "host.create"
	var data map[string]interface{}
	jsonstr := fmt.Sprintf(`{
        "host": "%s",
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "%s",
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "%s"
            }
        ],
        "templates": [
            {
                "templateid": "%s"
            }
        ],
        "inventory_mode": 0,
        "inventory": {
            "macaddress_a": "01234",
            "macaddress_b": "56768"
        }
	}`,hostname,hostip,groupid,templateid)
	json.Unmarshal([]byte(jsonstr),&data)
	return method,data
}

func HostDeleteData() (string,map[string]interface{}) {
	method := "host.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
     	"13",
        "32"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostDeleteByoptionData(hostidlist string) (string,map[string]interface{}) {
	method := "host.delete"
	var data map[string]interface{}
	jsonstr := fmt.Sprintf(`%s`,hostidlist)
	json.Unmarshal([]byte(jsonstr),&data)
	return method,data
}

func HostMassaddData() (string,map[string]interface{}) {
	method := "host.massadd"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "hosts": [
            {
                "hostid": "10160"
            },
            {
                "hostid": "10167"
            }
        ],
        "macros": [
            {
                "macro": "{$TEST1}",
                "value": "MACROTEST1"
            },
            {
                "macro": "{$TEST2}",
                "value": "MACROTEST2"
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostMassremoveData() (string,map[string]interface{}) {
	method := "host.massremove"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "hostids": ["69665", "69666"],
        "templateids_clear": "325"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostMassupdateData() (string,map[string]interface{}) {
	method := "host.massupdate"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "hosts": [
            {
                "hostid": "69665"
            },
            {
                "hostid": "69666"
            }
        ],
        "status": 0
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostUpdateData() (string,map[string]interface{}) {
	method := "host.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "hostid": "10126",
        "status": 0
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostGetData() (string,map[string]interface{}) {
	method := "host.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": ["hostid"],
        "selectGroups": "extend",
        "filter": {
            "host": [
                "Zabbix server"
            ]
        }
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostCreateByoption(url string,user string,password string,
	hostname string,hostip string,groupid string,templateid string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostCreateByoptionData(hostname,hostip,groupid,templateid)
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


func HostDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostDeleteByoption(url string,user string,password string,hostidlist string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostDeleteByoptionData(hostidlist)
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostMassadd(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostMassaddData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostMassremove(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostMassremoveData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostMassupdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostMassupdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}
